#ifndef GSTVIDEOPLAYER_H
#define GSTVIDEOPLAYER_H

#include <QObject>
#include <QWidget>
#include <gst/gst.h>
#include <gst/video/videooverlay.h>
#include <gst/app/gstappsink.h>
#include <gst/app/gstappsrc.h>
#include <QImage>

class GstVideoPlayer : public QObject {
    Q_OBJECT
public:
    explicit GstVideoPlayer(QWidget *videoWidget, QObject *parent = nullptr);
    ~GstVideoPlayer();

    void startPlayback(const QString &multicastAddress, quint16 port);
    void stopPlayback();

public:
    void handleGstMessage(GstMessage *msg);
    GstFlowReturn onNewFrame();
    static void onNeedData(GstAppSrc *appsrc, guint size, gpointer data);   //需要数据回调(本使用不关心)
    static void onEnoughData(GstAppSrc *appsrc, gpointer data);             //缓冲区满了回调(不关心)
    static GstFlowReturn onNewSample(GstAppSink *appsink, gpointer data);  // appsink回调

    void parseVideoInfo(GstCaps *caps);
    static GstPadProbeReturn capsProbeCallback(GstPad *pad, GstPadProbeInfo *info, gpointer user_data);

    QWidget *m_videoWidget;  // 用于显示视频的Qt控件
    GstElement *m_pipeline = nullptr;
    GstElement *m_videoSink = nullptr;
    GstElement *m_appSrc = nullptr;
    QImage currentImg;

    int m_width;
    int m_height;
    int m_fps;

public slots:
    void onUdpDataReceived(const QByteArray &datagram);

signals:
    void newImageAvailable(const QImage &img);  //收到的帧数据

    void videoInfoChanged(int width, int height, int fps);
};

#endif // GSTVIDEOPLAYER_H

